﻿#region License
// (c) Intergen.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
#endregion

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace TextGlow.Control.Utilities
{
  public static class ControlUtils
  {
    public static void SetBounds(this FrameworkElement e, Rect bounds)
    {
      SetBounds(e, bounds.X, bounds.Y, bounds.Width, bounds.Height);
    }

    public static void SetBounds(this Line l, Rect bounds)
    {
      SetLeft(l, bounds.X);
      SetTop(l, bounds.Y);

      l.X2 = bounds.Width;
      l.Y2 = bounds.Height;
    }

    public static void SetBounds(this FrameworkElement e, double left, double top, double width, double height)
    {
      SetLeft(e, left);
      SetTop(e, top);
      ScaleTransform renderTransform = e.RenderTransform as ScaleTransform;
      if (renderTransform != null)
      {
        width /= renderTransform.ScaleX;
        height /= renderTransform.ScaleY;
      }

      e.Width = width;
      e.Height = height;
    }

    public static void SetLeft(this UIElement e, double value)
    {
      if (value != GetLeft(e))
      {
        e.SetValue(Canvas.LeftProperty, value);
      }
    }

    public static void SetLocation(this UIElement e, Point location)
    {
      SetLeft(e, location.X);
      SetTop(e, location.Y);
    }

    public static void SetTop(this UIElement e, double value)
    {
      if (value != GetTop(e))
      {
        e.SetValue(Canvas.TopProperty, value);
      }
    }

    public static double GetLeft(this UIElement e)
    {
      return (double)e.GetValue(Canvas.LeftProperty);
    }

    public static double GetTop(this UIElement e)
    {
      return (double)e.GetValue(Canvas.TopProperty);
    }

    public static Point GetLocation(this UIElement e)
    {
      return new Point(GetLeft(e), GetTop(e));
    }

    public static Size GetScaledSize(this UIElement e)
    {
      Size size = e.RenderSize;
      ScaleTransform renderTransform = e.RenderTransform as ScaleTransform;
      if (renderTransform != null)
      {
        size.Width *= renderTransform.ScaleX;
        size.Height *= renderTransform.ScaleY;
      }
      return size;
    }
  }
}
